/**************************************************START OF FILE*****************************************************/





/*  ------------------------------------------------------------------------------------------------------------------
包含的头文件
*/
#include "stdint.h"
#include "xBSP_KeyScan.h"
#include "stm32f1xx_hal.h"





/*  ------------------------------------------------------------------------------------------------------------------
宏定义
*/

//一个滤波参数
#define NO_CHANGED_TIME 	2


/*  ------------------------------------------------------------------------------------------------------------------
变量定义
*/

/*按键扫描时所用的变量*/
static uint32_t KeyCurrent = 0 , KeyOld = 0 , KeyNoChangedTime = 0;
static uint32_t KeyPress = 0;
static uint32_t KeyDown = 0 , KeyUp = 0 , KeyLast = 0;
static uint32_t KeyCanChange = 1;

static void xSYS_KEY_Report(uint32_t CurrentKeyStatus);



/*
***********************************************************************************************************************
@函数名称 	： void xSYS_KEY_Scan(void)

@功    能 	： 按键描述程序10ms周期

@参    数 	： NONE

@返 回 值	： NONE

@作    者 	：YWJ
************************************************************************************************************************
*/
void xSYS_KEY_Scan(void)
{

	if(!KeyCanChange)return;
	/*************************按键扫描***********************/
	
	//收集按键状态
	KeyCurrent = (GPIOC->IDR & 0x3F);
	KeyCurrent = ~KeyCurrent;
	
	if(KeyCurrent!=KeyOld)
	{
		KeyNoChangedTime = 0;
		KeyOld = KeyCurrent;
		return;
	}
	else
	{
		KeyNoChangedTime++;
		if(KeyNoChangedTime>=NO_CHANGED_TIME)
		{
			KeyNoChangedTime = NO_CHANGED_TIME;
			KeyPress = KeyOld;
			KeyDown|=(~KeyLast)&(KeyPress);
			KeyUp|=KeyLast&(~KeyPress);
			KeyLast = KeyPress;
		}
	}
	xSYS_KEY_Report(KeyPress);
}



//HID报告发送缓冲
static uint8_t Tx_Report[4];
extern uint8_t USBD_HID_SendReport_MY(uint8_t *report,uint16_t len);

/*
*********************************************************************************************************************
@ Brief  : 返回报告描述符

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ:872180981)

@ Data   : 2021-09-10 14:36
*********************************************************************************************************************
*/
void xSYS_KEY_Report(uint32_t CurrentKeyStatus)
{
	static uint8_t LastreportValue = 0;
	uint8_t reportValue = 0;

	//上一曲
	if(CurrentKeyStatus & (0x01 << 0))
	{
		//bit2
		reportValue |= (0x01 << 2);
	}
	//播放/暂停
	if(CurrentKeyStatus & (0x01 << 1))
	{
		//bit0
		reportValue |= (0x01 << 0);
	}
	//下一曲
	if(CurrentKeyStatus & (0x01 << 2))
	{
		//bit1
		reportValue |= (0x01 << 1);
	}

	//静音
	if(CurrentKeyStatus & (0x01 << 3))
	{
		//bit5
		reportValue |= (0x01 << 5);
	}
	//音量减
	if(CurrentKeyStatus & (0x01 << 4))
	{
		//bit4
		reportValue |= (0x01 << 4);
	}
	//音量加
	if(CurrentKeyStatus & (0x01 << 5))
	{
		//bit3
		reportValue |= (0x01 << 3);
	}

	//发送报告给USB
	if(LastreportValue != reportValue)
	{
		LastreportValue = reportValue;
		Tx_Report[0] = reportValue;
		USBD_HID_SendReport_MY(Tx_Report,1);
	}
}



/**************************************************END OF FILE*****************************************************/
